package com.kcs.common.utils;

import java.util.Date;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import org.apache.log4j.Logger;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeBodyPart;

public class LogWriter {

	static Logger myLogger = null;

	public static void debug(String message, String logger){
		init(logger);
		myLogger.debug(message);
	}
	
	public static void info(String message, String logger){
		init(logger);
		myLogger.info(message);
	}
	
	public static void warning(String message, String logger){
		init(logger);
		myLogger.warn(message);
	}
	
	public static void error(String message, String logger){
		init(logger);
		myLogger.error(message);
	}

	private static void init(String logger) {	
		if (logger.equals("")){
			myLogger = Logger.getLogger(LogWriter.class);
		}else{
			myLogger = Logger.getLogger(logger);
		}
		
	}

	@SuppressWarnings("unused")
	private static void sendEmail(String error) {

		InternetAddress[] toAddr = null;
		String smtpServer = PropsUtils.getMessage(Constant.PROP_MAIL, "smtp-server");
		String fromAddress = PropsUtils.getMessage(Constant.PROP_MAIL, "from-address");
		String fromName = PropsUtils.getMessage(Constant.PROP_MAIL, "from-name");
		String subject = PropsUtils.getMessage(Constant.PROP_MAIL, "subject");
		String tmpAddress = PropsUtils.getMessage(Constant.PROP_MAIL, "to-address");

		try {
			// Prepare toAddr
			String[] addresses = tmpAddress.split(",");
			toAddr = new InternetAddress[addresses.length];
			for (int i = 0; i < addresses.length; i++) {
				toAddr[i] = new InternetAddress(addresses[i]);
			}
		
//			MailSender mailsender = new MailSender(smtpServer, "UTF-8");
//			mailsender.sendMail(fromAddress, fromName, toAddr, subject + "["
//					+ clientCode + "]", error, null);
			
			// Set SMPT server address
			Properties props = new Properties();
			props.put("mail.smtp.host", smtpServer);
			Session session = Session.getDefaultInstance(props, null);
			MimeMessage mimeMessage = new MimeMessage(session);
			
			// Set From mail address and From name
			mimeMessage.setFrom(new InternetAddress(fromAddress, fromName, "UTF-8"));
			
			// Set To mail address
			mimeMessage.setRecipients(Message.RecipientType.TO, toAddr);

			// Set subject
			mimeMessage.setSubject(subject, "UTF-8");

			// Create multipart object as content.
			Multipart content = new MimeMultipart();

			// Set 1st body part as text
			MimeBodyPart text = new MimeBodyPart();
			text.setText(error, "UTF-8");
			content.addBodyPart(text);

			// Set multipart object into message.
			mimeMessage.setContent(content);

			// Set send date.
			mimeMessage.setSentDate(new Date());
			// Perform sending.
			Transport.send(mimeMessage);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]) {
		LogWriter.info("Test", Constant.LOGGER_APPLICATION);
	 }  

}